<?php
set_time_limit(0);
ini_set('display_errors','on');
error_reporting(E_ALL);
ini_set('max_execution_time',0);

if(isset($_POST['act'])){
	session_start();
	$allow_sep = 300;
	if(isset($_SESSION['post_sep'])){
		if(time()-$_SESSION['post_sep'] < $allow_sep){
			header("location: " . $_SERVER['PHP_SELF']);
		}	
	}
	$_SESSION['post_sep'] = time();

	$origin_account = trim($_POST['origin_account'], ' ');
	$target_account = trim($_POST['target_account'], ' ');
	$user_info = array();

	$dbinfo = array(
		'host' => '127.0.0.1',
		'user' => 'root',
		'pass' => '123456',
		'dbname' => 'rhsd-heal'
	);

	$db = mysqli_connect($dbinfo['host'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['dbname']);
	$db->set_charset('UTF8');

	/*query account data*/
	$query = $db->query("select uid,email from rhsd_user where email IN ('" . $origin_account . "', '" . $target_account . "')");
	$account_result = $query->fetch_all(MYSQLI_ASSOC);
	foreach($account_result as $r){
		if($r['email'] == $origin_account){
		$user_info['origin']['account_id'] = $r['uid'];
		}
		elseif($r['email'] == $target_account){
			$user_info['target']['account_id'] = $r['uid'];
		}
		else{
			die('Illegal \'s account!');
		}
	}

	/*query school data*/	
	$school_query = $db->query("select id, fid from rhsd_school where fid IN (" . $user_info['origin']['account_id'] . ', ' . $user_info['target']['account_id'] . ')');
	$school_result = $school_query->fetch_all(MYSQLI_ASSOC);
	foreach($school_result as $r){
		if($r['fid'] == $user_info['origin']['account_id']){
			$user_info['origin']['school_id'] = $r['id'];
		}
		elseif($r['fid'] == $user_info['target']['account_id']){
			$user_info['target']['school_id'] = $r['id'];
		}
		else{
			die('Illegal \'s school id!');
		}
	}

	/*#BEGIN copy class data*/
	$sql = "select * from rhsd_class where school_id = " . $user_info['origin']['school_id'];
	$class_query = $db->query($sql);
	$class_result = $class_query->fetch_all(MYSQLI_ASSOC);
	foreach($class_result as $cr){
		if(!isset($cr['updatetime']) || empty($cr['updatetime'])){
			$cr['updatetime'] = 0;
		}

		/*add class data to target*/
		$sql = "insert into rhsd_class(`class_name`, `class_name1`, `class_name2`, `class_grade`, `class_type`, `school_id` ,`admin_id`, `is_del`, `ctime`, `updatetime`, `is_up`) values('" . $cr['class_name']. "','" . $cr['class_name1'] . "','" . $cr['class_name2'] . "','".$cr['class_grade']."','" . $cr['class_type'] . "','" . $user_info['target']['school_id'] . "','" . $cr['admin_id'] . "','" . $cr['is_del'] . "','" .  $cr['ctime'] . "','" . $cr['updatetime'] . "','" . $cr['is_up']. "')";
		$db->query($sql);
		/*add class data to target*/
		$target_class_id = $db->insert_id;
/*
		$health_query = $db->query("select * from rhsd_health_report where class_id = " . $cr['class_id'] . ' and fid = ' . $user_info['origin']['account_id']);
		$health_result = $health_query->fetch_all(MYSQLI_ASSOC);

		foreach($health_result as &$hs){
			//add health report data to target 
			if(!isset($hs['attach_id']) || empty($hs['attach_id'])){
				$hs['attach_id'] = 0;
			}

			if(!isset($hs['time']) || empty($hs['time'])){
				$hs['time'] = '0000-00-00';
			}
			$sql = "insert into rhsd_health_report(`rpt_name`,`year`,`is_report`,`test_type`,`type`,`fid`,`class_id`,`class_type`,`attach_id`,`detailed_info`,`comment`,`advice_img`,`status`,`time`) values('".$hs['rpt_name']."','".$hs['year']."','".$hs['is_report']."','".$hs['test_type']."','".$hs['type']."','".$hs['fid']."','".$target_class_id."','".$hs['class_type']."','".$hs['attach_id']."','".$hs['detailed_info']."','".$hs['comment']."','".$hs['advice_img']."','".$hs['status']."','".$hs['time']."')";
			$db->query($sql);
		}
*/
		//#BEGIN copy student data
		$student_query = $db->query("select * from rhsd_student where school_id = " . $user_info['origin']['school_id'] . ' and class_id = ' . $cr['class_id']);
		$student_result = $student_query->fetch_all(MYSQLI_ASSOC);
		foreach($student_result as $sr){
			//add student data to target
			$sql = "insert into rhsd_student(`stu_name`,`gender`,`birthday`,`birth_condition`,`birth_weight`,`into_campus_time`,`cid`,`school_id`,`class_id`,`family_id`,`relations`,`admin_id`,`is_del`) values('".$sr['stu_name']."','".$sr['gender']."','".$sr['birthday']."','".$sr['birth_condition']."','".$sr['birth_weight']."','".$sr['into_campus_time']."',0,'".$user_info['target']['school_id']."','".$target_class_id."','".$sr['family_id']."','".$sr['relations']."','".$sr['admin_id']."','".$sr['is_del'] ."')";
			$db->query($sql);
			$target_stu_id = $db->insert_id;
			$sql = "select * from rhsd_test_data where stu_id = " . $sr['stu_id'] . ' and class_id = ' . $cr['class_id'];

			$test_data_query = $db->query($sql);
			$test_data_result_multi = $test_data_query->fetch_all(MYSQLI_ASSOC);
			foreach($test_data_result_multi as $test_data_result){
				$sql = "insert into rhsd_test_data(`stu_id`,`year`,`date`,`test_number`,`modes_transportation`,`balanceBeam`,`continuousJump`,`tennis_throw`,`standing_long_jump`,`flexion`,`reentry_run`,`vision_right`,`vision_left`,`skinfold_abdomen`,`skinfold_shoulder_armor`,`skinfold_upper_arm`,`heart_rate`,`sit_height`,`weight`,`height`,`chest`,`is_update`,`is_report`,`class_id`,`class_type`,`is_del`,`admin_id`,`ctime`,`status`) values('".$target_stu_id."','".trim($test_data_result['year']," ")."','".$test_data_result['date']."','".$test_data_result['test_number']."','".$test_data_result['modes_transportation']."','".$test_data_result['balanceBeam']."','".$test_data_result['continuousJump']."','".$test_data_result['tennis_throw']."','".$test_data_result['standing_long_jump']."','".$test_data_result['flexion']."','".$test_data_result['reentry_run']."','".$test_data_result['vision_right']."','".$test_data_result['vision_left']."','".$test_data_result['skinfold_abdomen']."','".$test_data_result['skinfold_shoulder_armor']."','".$test_data_result['skinfold_upper_arm']."','".$test_data_result['heart_rate']."','".$test_data_result['sit_height']."','".$test_data_result['weight']."','".$test_data_result['height']."','".$test_data_result['chest']."','".$test_data_result['is_update']."','".$test_data_result['is_report']."','".$target_class_id."','".$test_data_result['class_type']."','".$test_data_result['is_del']."','".$test_data_result['admin_id']."','".$test_data_result['ctime']."','".$test_data_result['status']."')";
				$db->query($sql);
			}
		}
		//#END copy student data
	}
	/*#END copy class data*/
	die("All data copy complete!");
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
origin account:<input type="text" name="origin_account"/>
<br/>
target account:<input type="text" name="target_account"/>
<br/>
<input type="hidden" name="act" value="1" />
<input type="submit" name="_submit" value="submit"/>
</form>
